<?php
$sub_menu = "230550";
include_once('_common.php');

auth_check($auth[$sub_menu], "r");

if (empty($fr_date)) // 시작날짜 초기화
    $fr_date = date("Y-m-01", $g4['server_time']);

if (empty($to_date)) // 종료날짜 초기화
    $to_date = date("Y-m-t", $g4['server_time']);

$qstr = "fr_date={$fr_date}&to_date={$to_date}";

$g4['title'] = '아이디별 통계';

$colspan = 7;

include_once($g4['admin_path']."/admin.head.php");
?>

<a href="javascript:history.back()"><?php echo subtitle($g4['title'])?></a>

<script type="text/javascript">
function fcount_submit(ymd, sdate, edate) 
{
    var f = document.fcount;
    f.ymd.value = ymd;
    if (sdate) f.fr_date.value = sdate;
    if (edate) f.to_date.value = edate;
    f.action = "<?php echo $PHP_SELF?>";
    f.submit();
}
</script>

<form name=fcount method=get style="margin:0;">
<input type=hidden name="ymd">
<table width=100% cellpadding=3 cellspacing=1>
<tr>
    <td>
        기간 : 
        <input type='text' name='fr_date' size=11 maxlength=10 value='<?php echo $fr_date?>' class=ed> -
        <input type='text' name='to_date' size=11 maxlength=10 value='<?php echo $to_date?>' class=ed>
        &nbsp;
        <input type=button class=btn1 value=' 검색 ' onclick="fcount_submit('d');">
        <input type=button class=btn1 value=' 이번달 '
            onclick="fcount_submit('d', '<?php echo date("Y-m-01", $g4['server_time'])?>', '<?php echo date("Y-m-t", $g4['server_time'])?>');">
        <input type=button class=btn1 value=' 지난달 '
            onclick="fcount_submit('d', '<?php echo date("Y-m-01", strtotime("-1 month", strtotime("$fr_date 00:00:00")))?>',
                '<?php echo date("Y-m-t", strtotime("-1 month", strtotime("$fr_date 00:00:00")))?>');">
        <input type=button class=btn1 value=' 다음달 '
            onclick="fcount_submit('d', '<?php echo date("Y-m-01", strtotime("+1 month", strtotime("$fr_date 00:00:00")))?>',
                '<?php echo date("Y-m-t", strtotime("+1 month", strtotime("$fr_date 00:00:00")))?>');">
        <input type=button class=btn1 value=' 상반기 '
            onclick="fcount_submit('m', '<?php echo date("Y-01-01")?>', '<?php echo date("Y-06-30")?>');">
        <input type=button class=btn1 value=' 하반기 '
            onclick="fcount_submit('m', '<?php echo date("Y-07-01")?>', '<?php echo date("Y-12-31")?>');">
        <input type=button class=btn1 value=' 올해 '
            onclick="fcount_submit('m', '<?php echo date("Y-01-01")?>', '<?php echo date("Y-12-31")?>');">

        <input type=button class=btn1 value=' 전체 ' onclick="fcount_submit('d', '2000-01-01', '<?php echo date("Y-m-t", $g4['server_time'])?>')">
        &nbsp;&nbsp;
    </td>
</tr>
</table>
</form>


<table width=100% cellpadding=0 cellspacing=1 border=0>
<colgroup width=40>
<colgroup width=''>
<colgroup width=80>
<colgroup width=80>
<!--<colgroup width=100>-->
<colgroup width=80>
<colgroup width=250>
<tr><td colspan='<?php echo $colspan?>' class='line1'></td></tr>
<tr class='bgcol1 bold col1 ht center'>
    <td>번호</td>
    <td>아이디</td>
    <td><a href="<?php echo $_SERVER['PHP_SELF']?>?ord=cnt&<?php echo $qstr?>">결제횟수</a></td>
    <td><a href="<?php echo $_SERVER['PHP_SELF']?>?ord=cash&<?php echo $qstr?>"><?php echo $mw_cash['cf_cash_name']?></a></td>
    <!--<td><a href="<?php echo $_SERVER['PHP_SELF']?>?ord=pay&<?php echo $qstr?>">결제금액</a></td>-->
    <td>비율(%)</td>
    <td>그래프</td>
</tr>
<tr><td colspan='<?php echo $colspan?>' class='line2'></td></tr>
<?
$list = array();
$max = $sum = $sum_cash = 0;

$sql_select = " mb_id, cl_paytype, sum(cl_cash) as cash, sum(cl_payment) as pay, count(*) as cnt";
$sql_common = " from {$mw_cash['cash_list_table']} ";
$sql_search = " where cl_datetime between '{$fr_date} 00:00:00' and '{$to_date} 23:59:59' ";
$sql_search .= " and cl_cash > 0 and cl_payment > 0 and cl_status = '1' ";
$sql_search .= " and cl_paytype <> '포인트' and cl_paytype <> '서비스' and cl_paytype <> '' ";
$sql_group  = " group by mb_id";
if ($ord == "cnt")
    $sql_order = " order by cnt desc ";
else if ($ord == "pay")
    $sql_order = " order by pay desc ";
else
    $sql_order = " order by cash desc ";

switch ($ymd) {
    case "y":
        $sql_select .= ", left(cl_datetime, 4) as day ";
        break;
    case "m":
        $sql_select .= ", left(cl_datetime, 7) as day  ";
        break;
    default:
	$ymd = "d";
        $sql_select .= ", left(cl_datetime, 10) as day  ";
        break;
}

$sql = "select
	$sql_select
        $sql_common 
        $sql_search 
        $sql_group 
        $sql_order";
$qry = sql_query($sql);

$i = 0;
while ($row = sql_fetch_array($qry))
{
    if ($to_cash && $to_cash <= $row['cash']) {
        continue;
    }

    if ($fr_cash && $fr_cash > $row['cash']) {
        break;
    }

    $list[$i] = $row;

    if ($row['cash'] > $max) $max = $row['cash'];

    $sum += $row['cash'];
    $sum_cash += $row['cash'];

    $i++;
}

$total_count = count($list);

/*
$rows = $config['cf_page_rows'];
$total_page  = ceil($total_count / $rows);  // 전체 페이지 계산
if (!$page) $page = 1; // 페이지가 없으면 첫 페이지 (1 페이지)
$from_record = ($page - 1) * $rows; // 시작 열을 구함
*/

//for ($i=$from_record; $i<$from_record+$rows; $i++) { 
$c = 0;
for ($i=0; $i<$total_count; $i++) { 
    if ($i>=$total_count) break;

    $per1 = @round($list[$i]['cash'] / $sum * 100);
    $per2 = @round($list[$i]['cash'] / $max * 100);
    $day = $list[$i]['day'];

    $row = get_member($list[$i]['mb_id'], "mb_id, mb_nick, mb_email, mb_homepage");
    $mb_nick = get_sideview($row['mb_id'], $row['mb_nick'], $row['mb_email'], $row['mb_homepage']);

    $cash = "<a href='mw.cash.list.php?sfl=mb_id&stx={$list[$i]['mb_id']}";
    $cash.= "&to_date=$to_date&fr_date=$fr_date";
    //$cash.= "'>".$list[$i]['mb_id']."</a>";
    $cash.= "'>".number_format($list[$i]['cash'])."</a>";

    $n = $c+1;
    $l = ($c%2);
    echo "
    <tr class='list$l col1 ht center'>
	<td> {$n} </td>
	<td> {$mb_nick} </td>
	<td> ".number_format($list[$i]['cnt'])." </td>
	<td> ".$cash." </td>
	<!--<td> ".number_format($list[$i]['pay'])." </td>-->
	<td> {$per1}% </td>
	<td align=left> 
	    <img src='{$g4['admin_path']}/img/graph.gif' width='{$per2}%' height='18'>
	</td>
    <tr>";

    $c++;
}

if ($c == 0)
    echo "<tr><td colspan='{$colspan}' height=100 align=center>자료가 없습니다.</td></tr>"; 
else {
    echo "<tr><td colspan='{$colspan}' class='line2'></td></tr>
<tr class='bgcol1 bold col1 ht center'>
    <td colspan='3'>합계</td>
    <td>".number_format($sum_cash)."</td>
    <!--<td>".number_format($sum)."</td>-->
    <td>100%</td>
    <td>&nbsp;</td>
</tr>";
}

echo "<tr><td colspan='{$colspan}' class='line1'></td></tr>";
echo "</table>";

$page = get_paging($config['cf_write_pages'], $page, $total_page, $_SERVER['PHP_SELF']."?{$qstr}&domain={$domain}&page=");
if ($page) {
    echo "<table width=100% cellpadding=3 cellspacing=1><tr><td align=right>$page</td></tr></table>";
}

include_once($g4['admin_path']."/admin.tail.php");
